package com.nft.qz.commons.schedule.start;

import com.nft.qz.commons.schedule.ScheduleManager;
import com.nft.qz.commons.schedule.task.RedisDataTask;
import com.nft.qz.commons.schedule.task.UserVipPeriodTask;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @author 飞哥
 * @date 2023-08-02$ 23:24$
 */
@Component
@Slf4j
public class ApplicationStartedListener implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private ScheduleManager scheduleManager;
    @Autowired
    private RedisDataTask redisDataTask;
    @Autowired
    private UserVipPeriodTask userVipPeriodTask;

    @SneakyThrows
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 打印工程启动的输出日志
        printStartLog(event);
        // 启动两个定时任务
        //scheduleManager.startTask(IScheduledTask.REDIS_DATA_TOGGLE_KEY,redisDataTask,"*/1 * * * * ?");
        //scheduleManager.startTask(IScheduledTask.USER_VIP_PERDIOD_KEY,userVipPeriodTask,"*/5 * * * * ?");
    }


    /**
     * 打印启动日志
     *
     * @param event
     */
    private static void printStartLog(ApplicationReadyEvent event) {
        String serverPort = event.getApplicationContext().getEnvironment().getProperty("server.port");
        String serverUrl = String.format("http://%s:%s", "127.0.0.1", serverPort);
        log.info(AnsiOutput.toString(AnsiColor.BRIGHT_BLUE, "project server started at: ", serverUrl));
        log.info(AnsiOutput.toString(AnsiColor.BRIGHT_YELLOW, "project server has started successfully!"));
    }


}
